package api

import (
	"iris-starter/config"
	"iris-starter/datamodels"
	"iris-starter/pkg/errno"
	"time"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/jwt"
)

type AuthController struct {
	Ctx iris.Context
}

func (a *AuthController) Get() {
	a.Ctx.WriteString("auth index")
}

func (a *AuthController) GetToken() {
	secret := config.Conf.App.JwtSecret
	signer := jwt.NewSigner(jwt.HS256, []byte(secret), 24*time.Hour)
	claim := datamodels.AdminUserClaim{
		UserID:   1,
		UserName: "admin",
	}
	sign, err := signer.Sign(claim)
	if err != nil {
		a.Ctx.Application().Logger().Errorf("sign error:%v", err)
		JsonResponse(a.Ctx, errno.ErrSignParam, nil)
		return
	}
	JsonResponse(a.Ctx, nil, string(sign))
}
